package com.campus.controller;

import com.campus.common.ResponseResult;
import com.campus.model.Course;
import com.campus.model.LearningUnit;
import com.campus.service.CourseService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: 课程控制器类，用于处理课程相关的HTTP请求
 * @author: hjm
 * @date: 2024/11/22
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    /**
     * 创建课程
     *
     * @param course 课程对象
     * @return 创建结果
     */
    @PostMapping("/create")
    public ResponseResult<String> createCourse(@RequestBody Course course) {
        int result = courseService.createCourse(course);
        return result > 0 ? ResponseResult.ok("课程创建成功") : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "课程创建失败");
    }

    /**
     * 创建学习单元
     *
     * @param courseId    课程ID
     * @param learningUnit 学习单元对象
     * @return 创建结果
     */
    @PostMapping("/{courseId}/learn-units/create")
    public ResponseResult<String> createLearnUnit(@PathVariable int courseId, @RequestBody LearningUnit learningUnit) {
        learningUnit.setCourseId((long) courseId);
        int result = courseService.createLearnUnit(learningUnit);
        return result > 0 ? ResponseResult.ok("学习单元创建成功") : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "学习单元创建失败");
    }

    /**
     * 根据ID获取学习单元信息
     *
     * @param unitId 学习单元ID
     * @return 学习单元对象
     */
    @GetMapping("/learn-units/{unitId}")
    public ResponseResult<LearningUnit> getLearnUnitById(@PathVariable int unitId) {
        LearningUnit learningUnit = courseService.getLearnUnitById(unitId);
        return learningUnit != null ? ResponseResult.ok(learningUnit) : ResponseResult.error(HttpStatus.NOT_FOUND, "学习单元不存在");
    }

    /**
     * 根据课程ID获取学习单元列表
     *
     * @param courseId 课程ID
     * @return 学习单元列表
     */
    @GetMapping("/{courseId}/learn-units")
    public ResponseResult<List<LearningUnit>> getLearnUnitsByCourseId(@PathVariable int courseId) {
        List<LearningUnit> learningUnits = courseService.getLearnUnitsByCourseId(courseId);
        return ResponseResult.ok(learningUnits);
    }
}